import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import sys

# 把当前文件所在文件夹的父文件夹路径加入到PYTHONPATH
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from common import auto_barplot

# 读取数据
df = pd.read_excel("A/风化影响成分_百分比变化.xlsx")

# 获取每类文物变化最大的前 5 增加 & 减少的成分
top_changes = []

for category in df["文物类型"].unique():
    sub = df[df["文物类型"] == category]
    top_decrease = sub.nsmallest(5, "变化百分比")
    top_increase = sub.nlargest(5, "变化百分比")
    top_changes.append(pd.concat([top_decrease, top_increase]))

plot_df = pd.concat(top_changes)

auto_barplot(
    data=plot_df,
    x_col="成分",
    y_col="变化百分比",
    hue_col="文物类型",
    title="风化前后变化最大的成分（前5增加与前5减少）",
    save_path="A/风化成分_前五变化柱状图.png"
)